<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class View extends CI_Controller {
	
    function __construct() {
	parent::__construct();
    }
	
    function index($fid){
        $this->load->library("session_manager");
	$sm = $this->session_manager;
	$this->load->database();
	$this->load->model("Gps_log_files");
        
        $res = $this->Gps_log_files->get_log_file($sm->user['uid'], $fid);
        if ($res)
        {
            $gen_time = $res->gen_time;
            $filename = $res->filename;
            $kmlfile = $this->_build_path($sm->user['uid'],$gen_time,'kml')."/".$filename.".kml";
            
            //获取地图中心点
            $lat_scale = explode(",",$res->lat_scale);
            $lon_scale = explode(",",$res->lon_scale);
	    
	    //获取总里程
	    $distance = $res->distance;
	    //获取时间
	    $start = $res->start;
	    $end = $res->end;
	    $totaltime = abs($end - $start);
	    $timecost = floor($totaltime / 3600) . "小时" . floor(($totaltime % 3600) / 60) . "分" . ($totaltime % 60) . "秒";
	    //计算时速
	    $speed = $distance / ((float) $totaltime / (float)3600.0);
	    
	    //图表地址，文件存在则显示
	    $f = $this->_build_path($sm->user['uid'],$gen_time,'svg',false)."/".$filename."_spd.svg";
	    $spd_chart = file_exists($f) ? $this->_build_path($sm->user['uid'],$gen_time,'svg')."/".$filename."_spd.svg" : "";
	    $spd_hide = file_exists($f) ? "" : "hide";
	    
	    $f = $this->_build_path($sm->user['uid'],$gen_time,'svg',false)."/".$filename."_alt.svg";
	    $alt_chart = file_exists($f) ? $this->_build_path($sm->user['uid'],$gen_time,'svg')."/".$filename."_alt.svg" : "";
	    $alt_hide = file_exists($f) ? "" : "hide";

	    $f = $this->_build_path($sm->user['uid'],$gen_time,'svg',false)."/".$filename."_sat.svg";
	    $sat_chart = file_exists($f) ? $this->_build_path($sm->user['uid'],$gen_time,'svg')."/".$filename."_sat.svg" : "";
	    $sat_hide = file_exists($f) ? "" : "hide";

            
            if (isset($lat_scale[0]) && $lat_scale[1] && $lon_scale[0] && $lon_scale[1])
            {
                $c_lat = ((float) $lat_scale[0] + (float) $lat_scale[1]) / 2.0;
                $c_lon = ((float) $lon_scale[0] + (float) $lon_scale[1]) / 2.0;         
            }
            else
            {
                $c_lat = 0;
                $c_lon = 0;
            }
            
            //输出HTML表单
            $data = array(
                    "title" => "View - Info",
                    "jsarray" => array("resources/js/view.js"),
                    "extra" => '<script type="text/javascript" src="http://ditu.google.cn/maps/api/js?sensor=false&language=zh_cn"></script>'
            );
    
            $this->load->view("header", $data);
    
            $data = array("nickname" => $sm->user['nickname']);
            $this->load->view("menu", $data);
    
            $data = array(
                "kmlurl" => $kmlfile,
                "c_lat" => $c_lat,
                "c_lon" => $c_lon,
		"starttime" => @date("Y-m-d H:i:s", $start + TIME_OFFSET),
		"endtime" => @date("Y-m-d H:i:s", $end + TIME_OFFSET),
		"timecost" => $timecost,
		"distance" => $distance,
		"speed" => $speed,
		"shortname" => $res->shortname,
		"description" => $res->description,
		"spd_chart" => $spd_chart,
		"alt_chart" => $alt_chart,
		"sat_chart" => $sat_chart,
		"spd_hide" => $spd_hide,
		"alt_hide" => $alt_hide,
		"sat_hide" => $sat_hide,
		"max_spd" => $res->max_spd,
		"avs" => $res->avg_spd,
		"max_alt" => $res->max_alt,
		"min_alt" => $res->min_alt,
		"avg_alt" => $res->avg_alt
           );
            $this->load->view("view", $data);
            $this->load->view("footer");
        }
        else {
            
        }
        

    }

    function _build_path($uid,$timestamp,$type,$url=true){
            $dirs = array(
                    $uid,
                    $type,
                    @date("Y", $timestamp),
                    @date("m", $timestamp),
                    @date("d", $timestamp)
            );
            
	    if ($url) //生成URL或实际路径
		$path = base_url().STORAGE_PATH;
	    else
		$path = base_dir().STORAGE_PATH;
            foreach ($dirs as $dir){
                    $path .="/".$dir;
            }
                            
            return $path;
    }
}